//
//  popView.m
//  cityStar
//
//  Created by joyban on 15-1-7.
//  Copyright (c) 2015年 tiny. All rights reserved.
//
#define ScreenHeight [[UIScreen mainScreen] bounds].size.height
#define ScreenWidth [[UIScreen mainScreen] bounds].size.width

#import "popView.h"
#import "UIColor+FlatUI.h"

@implementation popView
@synthesize namelabel,numLabel,imageView,downbtn,upbtn,backView,xiugaiLabel,pricelabel,cellView;

-(id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setDrawView];
    }
    return self;
}

-(void)setDrawView
{
    backView = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
    [backView setBackgroundColor:[UIColor colorWithWhite:0.000 alpha:0.500]];
    [self addSubview:backView];
    [backView addTarget:self action:@selector(didBack_Aciton) forControlEvents:UIControlEventTouchUpInside];
    
    cellView = [[UIView alloc]initWithFrame:CGRectMake(0, ScreenHeight, ScreenWidth,142)];
    cellView.backgroundColor = [UIColor whiteColor];
    [self addSubview:cellView];
    
    xiugaiLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 15, 60, 16)];
    xiugaiLabel.font = [UIFont boldSystemFontOfSize:13];
    xiugaiLabel.textColor = [UIColor colorFromHexCode:@"FF9B5F"];
    xiugaiLabel.text = @"修改数量:";
    
    namelabel = [[UILabel alloc]initWithFrame:CGRectMake(75, 15, 225, 16)];
    namelabel.font = [UIFont boldSystemFontOfSize:13];
    namelabel.text = @"[绿叶]有机沙拉红米真采购价250g/包";
    namelabel.textColor = [UIColor colorFromHexCode:@"333333"];
    
    imageView = [[UIImageView alloc]initWithFrame:CGRectMake(10, 42, 85, 67)];
    imageView.image = [UIImage imageNamed:@"商户列表_img"];
    
    UIImageView *imageBackNumView = [[UIImageView alloc]initWithFrame:CGRectMake(115, 65, 100, 30)];
    imageBackNumView.image = [UIImage imageNamed:@"numberbg"];
    
    
    downbtn = [[UIButton alloc]initWithFrame:CGRectMake(115, 65, 30, 30)];
    downbtn.userInteractionEnabled = YES;
//   downbtn.backgroundColor = [UIColor redColor];
    [downbtn setImage:[UIImage imageNamed:@"minus"] forState:UIControlStateNormal];
    [downbtn setImage:[UIImage imageNamed:@"minus_touch"] forState:UIControlStateHighlighted];
    [downbtn addTarget:self action:@selector(didDown_Aciton) forControlEvents:UIControlEventTouchUpInside];

    numLabel = [[UITextField alloc]initWithFrame:CGRectMake(160, 65, 45, 30)];
    numLabel.font = [UIFont systemFontOfSize:14];
    numLabel.text = @"1";
    numLabel.borderStyle = UITextBorderStyleNone;
    numLabel.delegate = self;
    numLabel.returnKeyType = UIReturnKeyDone;
    
    upbtn = [[UIButton alloc]initWithFrame:CGRectMake(185, 65, 30, 30)];
    [upbtn setImage:[UIImage imageNamed:@"add_shop"] forState:UIControlStateNormal];
    [upbtn setImage:[UIImage imageNamed:@"add_touch"] forState:UIControlStateHighlighted];
    [upbtn addTarget:self action:@selector(didUp_Aciton) forControlEvents:UIControlEventTouchUpInside];

    pricelabel = [[UILabel alloc]initWithFrame:CGRectMake(215,70 , 88, 18)];
    pricelabel.font = [UIFont systemFontOfSize:18];
    pricelabel.text = @"￥180";
    pricelabel.userInteractionEnabled = YES;
    pricelabel.textAlignment = NSTextAlignmentRight;
    pricelabel.textColor = [UIColor colorFromHexCode:@"FF9B5F"];

    [cellView addSubview:xiugaiLabel];
    [cellView addSubview:namelabel];
    [cellView addSubview:imageView];
    [cellView addSubview:imageBackNumView];
    [cellView addSubview:numLabel];
    [cellView addSubview:downbtn];
    [cellView addSubview:upbtn];
    [cellView addSubview:pricelabel];
}

-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}

-(void)didDown_Aciton
{
    int num = [numLabel.text intValue];
    if(num >1)
    {
        num = num - 1;
    }
    numLabel.text = [NSString stringWithFormat:@"%d",num];
    pricelabel.text = [NSString stringWithFormat:@"￥%.2f",self.price*num];
}

-(void)didUp_Aciton
{
    int num = [numLabel.text intValue];
    num = num + 1;
    numLabel.text = [NSString stringWithFormat:@"%d",num];
    pricelabel.text = [NSString stringWithFormat:@"￥%.2f",self.price*num];
}


-(void)setNamelabel:(NSString *)name setImage:(UIImage *)image setPrice:(NSString *)price
{
    namelabel.text = name;
    imageView.image = image;
    pricelabel.text = price;
}


-(void)didBack_Aciton
{
    [self.numLabel resignFirstResponder];
    [UIView animateWithDuration:0.3f animations:^{
        [cellView setFrame:CGRectMake(0, ScreenHeight, ScreenWidth,142)];
        self.alpha = 0;
    }];
    NSString *num = [NSString stringWithFormat:@"%i",[self.numLabel.text intValue] - [self.number intValue]];
    if ([self.delegate respondsToSelector:@selector(senderBackClick:)]) {
        [self.delegate senderBackClick:num];
    }
}

@end
